import baseUrl from "./baseUrl"
import store from "../store/index.js"

// import {
// 	smallProgramLogin,
// 	smallProgramLoginByPhone
// } from "@/api/login/login.js"
const isout = false;

var request = function(config) {
	uni.showLoading({
		title: 'Loading'
	})
	return new Promise((resolve, rejected) => {
		const tokens = uni.getStorageSync('massageToken');


		var setting = {
			url: baseUrl + config.url,
			method: config.method ? (config.method).toUpperCase() : "GET",

			data: config.data,

			header: {
				// "Access-Control-Allow-Origin": '*',
				"Content-Type": "application/json",
				'Authorization': tokens ? 'bearer ' + tokens : '',
				'lang': store.state.currentLanguage,
				...config?.header,
			},
			timeout: 40000,

			success(res) {
				uni.hideLoading();

				if (res.data.code == 0) {
					resolve(res.data);
				} else if (res.data.code == 501) {
					uni.navigateTo({
						url: "/pages/banner"
					})
				} else {

					if (res.data.code != 0) {
						uni.showToast({
							title: res.data.msg,
							icon: "none"
						})
					}
					rejected(res)
				}
			},
			fail(res) {
				uni.hideLoading();
				rejected(res)
			}
		}
		uni.request(setting)
	})

};
export default request